var utils = require("../../utils/util.js");
var app = getApp();
Page({
  data: { 
    payType: 1,
    user_balance:0,
    actual:0
  },
  onLoad: function (options){
    var that=this;
    if (options.user_balance){
      this.setData({
        user_balance: options.user_balance,
        actual: options.actual,
        order_id: options.order_id,
        order_no: options.order_no
      });
    }else{
      wx.showLoading();
      wx.request({
        url: utils.dominame + '/api/v1/commission/mybalanceDetail',
        data: {
          time_interval:1,
          token: app.globalData.token,
          pageSize:1,
          pageIndex:1
        },
        method: "GET",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"  //post
        },
        complete: function (res) {
          wx.hideLoading();
        },
        success: function (res) {
          console.log(res);
          if (res.data.code == 0) {
            that.setData({
              user_balance: res.data.data.user_balance,
              actual: options.actual,
              order_id: options.order_id,
            });
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none"
            })
          }
        }
      })
    }
  },
  radioChange: function (e) {
    console.log(e.detail.value);
    this.setData({
      payType: e.detail.value
    });
  },
  payWx: function (param, subject) {
    wx.request({
      url: "https://wx.sencegroup.com/xingchen/api/payment/weixinpay/h5/makePreOrder",
      data: {
        token: app.globalData.token,
        redpacket_id: param,
        subject: subject,
        pay_type: 1,
        xcx_openId: app.globalData.openId
      },
      method: 'POST',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (answer) {
        console.log(answer);
        if (answer.data.code == 0) {
          wx.requestPayment({
            timeStamp: answer.data.data.timeStamp,
            nonceStr: answer.data.data.nonceStr,
            package: answer.data.data.packageValue,
            signType: answer.data.data.signType,
            paySign: answer.data.data.sign,
            success: function () {
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                mask: true,
              });
              setTimeout(() => {
                wx.navigateBack();
              }, 1000)
            },
            fail: function () {
              wx.showToast({
                title: '支付失败',
                icon: 'none'
              })
            }
          });
        } else {
          wx.showToast({
            title: answer.data.msg,
          })
        }
      }
    })
  },
  payMoney:function(){
    var that = this;
    if (this.data.payType==1){
      wx.showLoading();
      wx.request({
        url: utils.dominame + '/api/v1/order/balancePay',
        data: {
          token: app.globalData.token,
          order_ids: that.data.order_id,
          pay_type: 9
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"  //post
        },
        complete: function (res) {},
        success: function (res) {
          console.log(res);
          wx.hideLoading();
          if (res.data.code == 0) {
            wx.redirectTo({
              url: '../paySuccess/paySuccess?order_no=' + res.data.data.order_no + '&pay_time=' + res.data.data.pay_time + '&pay_type=' + res.data.data.pay_type,
            });
          } else {
            wx.hideLoading();
            wx.showToast({
              title: res.data.msg,
              icon: "none"
            })
          }
        }
      })
    }else{
      
    }
  }
})